import { Service } from "egg";
import { Types } from "mongoose";
import { PositionProps } from "../model/Position";

export default class PositionService extends Service {
  /**
   * 根据传入名称查询
   */
  public findByName(position_name: string) {
    return this.ctx.model.Position.findOne({ position_name });
  }

  /**
   * 查找列表
   */
  public findList() {
    return this.ctx.model.Position.find();
  }

  /**
   * 添加数据
   * @param data 测试数据
   */
  public async add(data) {
    const { ctx } = this;
    const { user: userInfo } = ctx.state;
    const _data: Partial<PositionProps> = {
      ...data,
      company_id: Types.ObjectId(userInfo.company),
    };
    return await ctx.model.Position.create(_data);
  }
}
